import { _decorator } from 'cc';
import { LoadPanelBase } from './LoadPanelBase';
import { PanelModel } from './PanelModel';
import { CallBackHandler } from '../Model/PoolObj/CallBackHandler';
import { LoadingManager } from '../Manager/LoadingManager';
import { LayerManager } from '../Manager/LayerManager';
// import { TurningAni } from './TurningAni';
import { Task } from '../../Util/CustomTask';
// import { BattlePlayerAttr } from '../../Game/Main/Battle/Role';
// import { Player } from '../../Game/Main/Battle/Player';
const { ccclass, property } = _decorator;

class OpenLoadModel extends PanelModel {
  className: string = 'OpenLoadModel';
  readonly loadRes: string = 'resources/loading/OpenLoadPanel';
  loadCallback: Task.next[] = [];
  readyOpenHander: Task.next;
  showAni: boolean = true;
  basePanel: OpenLoadPanel;
}

export const OpenLoadModel_Ins = OpenLoadModel.Ins();

@ccclass('OpenPanelLoad')
export class OpenLoadPanel extends LoadPanelBase {
  isPierce: boolean = false;
  blackMask: number = 0;
  clickBackClose: boolean = false;

  Complete(): void {}

  onRender(): void {
    OpenLoadModel_Ins.basePanel = this;
  }

  // isFirst: boolean = true;

  onReady(): void {
    OpenLoadModel_Ins.curPanel = this;
  }

  loadPanel(resPath: string, prop?: Record<any, any>): void {
    if (resPath === '') return;
    this.saveProp = prop;
    try {
      const res = LoadingManager.Ins().getResource(resPath);
      this.onComplete([res]);
      // OpenLoadModel_Ins.showAni && this.Turning && this.Turning.show(OpenLoadModel_Ins.showAni);
    } catch {
      // this.Turning.node.active = OpenLoadModel_Ins.showAni;
      // this.Turning && this.Turning.show(OpenLoadModel_Ins.showAni);
      this.node.active = true;
      this.tempRate = 0;
      this.loadingRate = 0;
      LoadingManager.Ins().loadResource(
        resPath,
        this.onProgress,
        CallBackHandler.pop((res) => {
          this.onComplete(res);
        }, this)
      );
      // LoadingManager.Ins().loadResource(resPath, this.onProgress);
    }
  }

  protected update(dt: number): void {}
  /** 进度 */
  protected onProgress(rate) {}

  protected onComplete(res): void {
    OpenLoadModel_Ins.readyOpenHander && OpenLoadModel_Ins.readyOpenHander();
    OpenLoadModel_Ins.readyOpenHander = null;
    // this.Turning && this.Turning.resumeAni();
    // try {
    LayerManager.Singleton.loadPanelComplete(res, this.saveProp);
    // } catch {
    //   console.log('onComplete', res);
    // }
    this.saveProp = null;
    OpenLoadModel_Ins.showAni = false;
    this.node.active = false;
  }
}
